package Coffee.service;

import Coffee.bean.Order;
import Coffee.bean.User;
import Coffee.dao.OrderDao;
import Coffee.dao.OrderDaoImpl;
import Coffee.dao.UserDao;
import Coffee.dao.UserDaoImpl;

import java.util.List;
import java.util.Scanner;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    OrderDao orderDao = new OrderDaoImpl();


    @Override
    public void insertUser(User user) {
        User user1 = userDao.queryUser(user.getUsername());
        if (user1 != null) {
            System.out.println("用户名重复");
            System.out.println("添加失败");
        } else {
            userDao.insertUser(user);
        }

    }

    @Override
    public User queryUser(String username) {
        User user = userDao.queryUser(username);
        if (user == null) {
            System.out.println("不存在该用户");
        }
        return user;
    }

    @Override
    public void delUserbyName(String username) {
        User user = userDao.queryUser(username);
        if (user == null) {
            System.out.println("不存在该用户");
        } else {
            int i = userDao.delUserbyName(username);
            if (i == 1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }

        }
    }

    @Override
    public int updateUser(String oldname, String newname, String userpwd) {
        User user = queryUser(oldname);
        int i = userDao.updateUser(newname, userpwd, user.getUserid());
        if (i == 1) {
            System.out.println("修改成功");
        } else System.out.println("修改失败");
        return i;
    }

    @Override
    public List<User> queryAll() {
        return userDao.queryAll();
    }

    @Override
    public boolean login(String username, String userpwd) {
        User login = userDao.login(username, userpwd);
        if (login == null) {
            System.out.println("用户名或者密码错误");
        } else System.out.println("登录成功");
        return (login!=null);//验证错误返回false，验证成功返回true
    }

    @Override
    public void insertMoney(int money, String username) {
        User user = queryUser(username);
        if (user != null) {
            int i = userDao.insertMoney(money, username);
            boolean success = (i != 0);
            System.out.println(success ? "充值成功" : "充值失败");
        } else System.out.println("充值失败");
    }

    @Override
    public void queryMoneyByUsername(String username) {
        User user = queryUser(username);
        if (user != null) {
            System.out.println("用户" + username + "的余额：" + userDao.queryMoneyByUsername(username));
        }
    }

    @Override
    public void updateMoney(int money, String username) {//传入的是消费金额，先查询余额判断
        int left = userDao.queryMoneyByUsername(username);//余额
        if (left >= money) {
            userDao.updateMoney(money, username);
            System.out.println("结算成功");
            queryMoneyByUsername(username);
        } else System.out.println("余额不足");

    }

    @Override
    public void payOrder(String orderid, String username) {
        //A.先判断余额，余额不足报错返回，余额足弹出是否支付
        //B支付则改变订单状态为制作中，User钱包--
        try {
            String state = orderDao.queryOrderByOrderId(orderid).getOrderstate();
            int left = userDao.queryMoneyByUsername(username); // 余额
            int pay = orderDao.queryOrderByOrderId(orderid).getCoftotal(); // 订单价格
            if (state.equals("已取消")) {//先把已取消的排除
                System.out.println("该订单已经取消");
                return;
            }
            if (!state.equals("待支付")) {//剩下的两个制作中 和已完成都是支付了的情况
                System.out.println("订单已支付");
                System.out.println(orderDao.queryOrderByOrderId(orderid));
                return;
            }
            if (left < pay) {
                System.out.println("余额不足");
            } else {
//                try {
                    Scanner sc = new Scanner(System.in);
                    System.out.println("余额：" + left + "订单价格" + pay);
                    System.out.println("请问是否确认支付Y/N");
                    String p1 = sc.next();
                    switch (p1.toUpperCase()) {
                        case "Y":
                            updateMoney(pay, username);
                            orderDao.updateOrderState("制作中", orderid);
                            break;
                        case "N":
                            System.out.println("订单取消支付");
                            break;
                        default:
                            System.out.println("无效输入，请输入Y或N");
                            break;
                    }
//                }
            }

        } catch (Exception e) {
            System.out.println("支付过程中发生错误: " + e.getMessage());
            // 记录日志或采取其他错误处理措施
        }
    }

    @Override
    public void getCoffee(String orderid) {//前提已经拿到筛选的订单表（制作中）
        Order order = orderDao.queryOrderByOrderId(orderid);
        String s1 = order.getOrderstate();
        int i = 0;
        if (s1.equals("制作中")) {
            i = orderDao.updateOrderState("已完成", orderid);
        }
        boolean get1 = (i == 1);
        System.out.println(get1 ? "拿到coffee" : "拿取失败");

    }

    @Override
    public void repay(String orderid) {
        Order order = orderDao.queryOrderByOrderId(orderid);
        String state = order.getOrderstate();
        switch (state) {
            case "待支付"://直接改状态
                int i = orderDao.updateOrderState("已取消", orderid);
                boolean get1 = (i == 1);
                System.out.println(get1 ? "退单成功" : "退单失败");
                break;
            case "制作中"://改状态和返钱
                int j = orderDao.updateOrderState("已取消", orderid);
                int i1 = userDao.insertMoney(order.getCoftotal(), order.getUsername());
                if (j == 1 & i1 == 1) {
                    System.out.println("退单成功");
                } else {
                    System.out.println("退单失败");
                    return;
                }
                break;
            case "已完成":
            case "已取消":
                System.out.println("订单已经完成或取消");
                break;

        }
    }

}
